package com.wxg.gui.baidu;

import static com.wxg.httpclient.ObtainAudioFromBaiduFanyi.AudioType.USA;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.wxg.gui.GuiNote;
import com.wxg.gui.WordSaveAndQuery;
import com.wxg.httpclient.ObtainAudioFromBaiduFanyi.AudioType;

public class WordInputPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1912016136409798948L;
	private JTextField textField;
	private final Action action = new QueryAction();
	private final Action webAction = new GoWebAction();
	
	private static GuiNote g = new GuiNote("=== 注释开始 ===");
	
	private Object parent;

	/**
	 * Create the panel.
	 */
	public WordInputPanel(Object parent) {
		this.parent = parent;
		
		setLayout(new BorderLayout(0, 0));
		
		JButton btnQuery = new JButton("111");
		
//		btnQuery.setAction(action);
		btnQuery.setAction(webAction);
		
		add(btnQuery, BorderLayout.EAST);
		
		textField = new JTextField();
		
		final BaiduFanyiWordAudioApp app = (BaiduFanyiWordAudioApp)parent;
		
		/**
		 * 监听: 文本框的回车触发！
		 */
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = e.getActionCommand(); g.a("getActionCommand() 获取的就是文本框中输入的值...");
				if(null != text){
					WordSaveAndQuery wsaq = new WordSaveAndQuery();
					AudioType audioType = USA;
					wsaq.all(text, app, audioType);
				}
			}
		});
		
		add(textField, BorderLayout.CENTER);
		textField.setColumns(10);

	}

	/**
	 * `actionPerformed`只是简单的输出信息,没有实际作用
	 * @author wxg
	 *
	 */
	private class QueryAction extends AbstractAction {
		private static final long serialVersionUID = -4818696590824238200L;
		public QueryAction() {
			putValue(NAME, "Query"); g.a("这里的`NAME`会替换掉Button中的text: 参考javadoc...");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			System.out.println( "===e.getActionCommand()===" );
			System.out.println( e.getActionCommand() );
			System.out.println("===e.getSource().toString()===");
			System.out.println( e.getSource().toString() );
//			getRootPane()
//			getTopLevelAncestor()
			BaiduFanyiWordAudioApp app = (BaiduFanyiWordAudioApp)parent;
			
			JTextArea area = app.getWordExpResultPanel().getResultTextArea();
			area.setText( g.getNotes() );
			
//			JLabel label = app.getWordExpResultPanel().getResultLabel();
//			label.setText( guinote.getNotes() );
		}
	}
	
	private class GoWebAction extends AbstractAction {
		private static final long serialVersionUID = 3622062873086050557L;
		public GoWebAction() {
			putValue(NAME, "Web"); g.a("这里的`NAME`会替换掉Button中的text: 参考javadoc...");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			System.out.println( "===e.getActionCommand()===" );
			System.out.println( e.getActionCommand() );
			System.out.println("===e.getSource().toString()===");
			System.out.println( e.getSource().toString() );
//			getRootPane()
//			getTopLevelAncestor()
			BaiduFanyiWordAudioApp app = (BaiduFanyiWordAudioApp)parent;
			
			JTextArea area = app.getWordExpResultPanel().getResultTextArea();
			area.setText( g.getNotes() );
			
//			JLabel label = app.getWordExpResultPanel().getResultLabel();
//			label.setText( guinote.getNotes() );
		}
	}
	
	
}
